package com.lbw.factory.simplefactory.order;

import com.lbw.factory.simplefactory.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/9/5 11:41
 */
public class OrderPizza {
    private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    // public OrderPizza() {
    //     Pizza pizza = null;
    //     String orderType = "";
    //     do {
    //         orderType = getType();
    //         if ("greek".equals(orderType)) {
    //             pizza = new GreekPizza("希腊披萨");
    //         } else if ("cheese".equals(orderType)) {
    //             pizza = new ChessPizza("芝士披萨");
    //         } else {
    //             break;
    //         }
    //         pizza.prepare();
    //         pizza.bake();
    //         pizza.cut();
    //         pizza.box();
    //     } while (true);
    //     try {
    //         reader.close();
    //     } catch (IOException e) {
    //         e.printStackTrace();
    //     }
    // }
    private SimplePizzaFactory simplePizzaFactory;
    private Pizza pizza;

    public OrderPizza(SimplePizzaFactory simplePizzaFactory) {
        setSimplePizzaFactory(simplePizzaFactory);
    }

    public void setSimplePizzaFactory(SimplePizzaFactory simplePizzaFactory) {
        String type = "";
        this.simplePizzaFactory = simplePizzaFactory;

        do {
            type = getType();
            this.pizza = simplePizzaFactory.createPizza(type);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("没有这种披萨");
                break;
            }
        } while (true);
    }

    public Pizza getPizza() {
        return pizza;
    }

    private String getType() {

        System.out.println("input pizza type: ");
        String type = null;
        try {
            type = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return type;
    }
}
